package com.ai.aicontact.execmgn.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.aicontact.busi.entity.CriticalPointCatagory;
import com.ai.aicontact.busi.entity.CriticalPointDef;
import com.ai.aicontact.busi.service.CriticalPointService;
import com.ai.aicontact.busi.vo.CriticalPointCatagoryVO;
import com.ai.aicontact.common.vo.Grid;
import com.ai.aicontact.common.vo.Json;
import com.ai.aicontact.common.vo.PageFilter;
import com.ai.aicontact.common.vo.ZTree;

@Controller
@RequestMapping("/contactmgn/point")
public class PointMgnController {

	@RequestMapping("/{jspname}")
	public String web(@PathVariable("jspname") String jspname) {
		return "/contactmgn/point/" + jspname;
	}

	
	@RequestMapping("/pointcatagory")
	public String pointcatagory(HttpServletRequest request, Long catagoryId) throws Exception {
		CriticalPointCatagory pointCatagory;
		if (catagoryId != null && catagoryId > 0) {
			 pointCatagory = criticalPointService.getCriticalPointCatagoryById(catagoryId);
			
			if(pointCatagory.getParentId()!=null && pointCatagory.getParentId()>0){
				CriticalPointCatagory pPointCatagory = criticalPointService.getCriticalPointCatagoryById(pointCatagory.getParentId());
				if(pPointCatagory!=null)
					pointCatagory.setParentName(pPointCatagory.getCatagoryName());
			}
			
		}else{
			 pointCatagory =  new CriticalPointCatagory();
			 pointCatagory.setCatagoryLevel(0);
			 pointCatagory.setSortBy(1);
			 pointCatagory.setParentId(0L);
		}
		request.setAttribute("pointCatagory", pointCatagory);
		return "/contactmgn/point/pointcatagory";
	}
	
	
	@Autowired
	private CriticalPointService criticalPointService;

	@ResponseBody
	@RequestMapping("queryPointCatagory")
	public Grid queryPointCatagory(CriticalPointCatagory criticalPointCatagory) throws Exception {
		Grid grid = new Grid();
		List<CriticalPointCatagoryVO> rows = criticalPointService.getCriticalPointCatagory(criticalPointCatagory);
		grid.setRows(rows);
		return grid;
	}

	@ResponseBody
	@RequestMapping("getPointCatagoryTree")
	public Json getPointCatagoryTree() throws Exception {
		Json json = new Json();
		try {
			List<ZTree> list = new ArrayList<ZTree>();
			List<CriticalPointCatagoryVO> catagoryList = criticalPointService.getCriticalPointCatagory(null);
			for (CriticalPointCatagoryVO catagory : catagoryList) {
				ZTree ztree = new ZTree();
				ztree.setId(catagory.getCatagoryId().longValue());
				ztree.setName(catagory.getCatagoryName());
				//ztree.setLevel(catagory.getCatagoryLevel());
				if (catagory.getParentId() != null && catagory.getParentId().longValue() > 0) {
					ztree.setParent(true);
					ztree.setpId(catagory.getParentId().longValue());
				} else {
					ztree.setParent(false);
				}
				list.add(ztree);
			}
			json.setObj(list);
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}

		return json;
	}

	@ResponseBody
	@RequestMapping("savePointCatagory")
	public Json savePointCatagory(CriticalPointCatagory criticalPointCatagory) throws Exception {
		Json json = new Json();
		try {
			criticalPointService.saveCriticalPointCatagory(criticalPointCatagory);
			json.setObj(criticalPointCatagory);
			json.setSuccess(true);
			json.setMsg("保存成功");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}



	@ResponseBody
	@RequestMapping("delPointCatagory")
	public Json delPointCatagory(CriticalPointCatagory criticalPointCatagory) throws Exception {
		Json json = new Json();
		try {
			criticalPointService.delCriticalPointCatagory(criticalPointCatagory);
			json.setSuccess(true);
			json.setMsg("删除成功");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}

	
	
	@ResponseBody
	@RequestMapping("delPoint")
	public Json delPoint(CriticalPointDef criticalPointDef) throws Exception {
		Json json = new Json();
		try {
			criticalPointService.delCriticalPointDef(criticalPointDef);
			json.setSuccess(true);
			json.setMsg("删除成功");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}

	
	@ResponseBody
	@RequestMapping("savePoint")
	public Json savePoint(CriticalPointDef criticalPointDef) throws Exception {
		Json json = new Json();
		try {
			criticalPointService.saveCriticalPointDef(criticalPointDef);
			json.setObj(criticalPointDef);
			json.setSuccess(true);
			json.setMsg("保存成功");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping("queryPoint")
	public Grid queryPoint(CriticalPointDef criticalPointDef,PageFilter pf) throws Exception {
		Grid grid = new Grid();
		List<CriticalPointDef> rows = criticalPointService.getCriticalPointDef(criticalPointDef, pf);
		grid.setRows(rows); 
		Long count = criticalPointService.countCriticalPointDef(criticalPointDef);
		grid.setTotal(count);
		return grid;
	}

	
}
